/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logica.carro;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.carro.Carro;
import model.carro.CarroDAO;
import model.cliente.Cliente;
import model.cliente.ClienteDAO;

/**
 *
 * @author davi
 */
public class AdicionaCarro implements logica.Logica{

    @Override
    public void executa(HttpServletRequest req, HttpServletResponse res) throws Exception {
        
        String placa = req.getParameter("placa");
        String modelo = req.getParameter("modelo");
        String marca = req.getParameter("marca");
        String cor = req.getParameter("cor");
        
        String clienteID = req.getParameter("clienteID");
        
        ClienteDAO clienteDAO = new ClienteDAO();
        Cliente cliente = clienteDAO.buscaClienteDetalhes(clienteID);
                
        Carro carro = new Carro();
        
        
        carro.setPlaca(placa);
        carro.setModelo(modelo);
        carro.setMarca(marca);
        carro.setCor(cor);
        carro.setCliente(cliente);
        
        req.setAttribute("operacao", "salvar carro");
        
        try{
            CarroDAO dao = new CarroDAO();
            dao.salvar(carro);
            req.setAttribute("carro", carro);
            RequestDispatcher rd = req.getRequestDispatcher("sucesso.jsp");
            rd.forward(req, res);
            
            
        } catch(Exception e ){
        
        e.printStackTrace();
        RequestDispatcher rd = req.getRequestDispatcher("erro.jsp");
        rd.forward(req, res);
        }
        
        
    }
    
}
